﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;

namespace MyImage
{
    public partial class Export2XML : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = DataAcess.GetAllImage(false);
                // Get a FileStream object
                StreamWriter xmlDoc = new StreamWriter(Server.MapPath("~/FileTest/XMLData.xml"), false);
                // Apply the WriteXml method to write an XML document
                ds.WriteXml(xmlDoc);
                xmlDoc.Close();
                lblMessage.Text = "Successful !";

                ////show xml
                //DataSet ds1 = new DataSet();

                //StreamReader xmlReader = new StreamReader(Server.MapPath("~/FileTest/XMLData.xml"), false);
                //ds1.ReadXml(xmlReader);
                //xmlReader.Close();
                //GridView1.DataSource = ds1;
                //GridView1.DataBind();

                
               


            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }



        protected void Button2_Click1(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = new DataSet();
                StreamReader xmlReader = new StreamReader(Server.MapPath("~/FileTest/XMLData.xml"), false);
                ds.ReadXml(xmlReader);

                xmlReader.Close();

                foreach (DataTable dt in ds.Tables)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        int img_pk = int.Parse(dr["img_pk"].ToString());

                        System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                        byte[] imageByte = encoding.GetBytes(dr["img_data"].ToString());

                        DataAcess.SaveImage(ref img_pk, imageByte, true);

                    }
                }
                lblMessage.Text = "Successful !";

                Response.Redirect("~/ShowDataCopy.aspx");
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }
    }
}
